PHP Custom Recursive Function Realization of Array to JSON Function [Support GBK Encoding]

  • 2021-10-24 19:16:33
  • OfStack

This article describes the PHP self-defined recursive function to achieve array to JSON function. Share it for your reference, as follows:

Question:

Due to a recent project to provide interfaces to other companies, feed them GBK encoded json data, but there is a problem in PHP json_encode Encryption functions only support utf-8 encoding, which is rather embarrassing. Our data is encoded by GBK, and the data format required by the receiver is also encoded by GBK. At first, I wanted to convert the data to utf-8 encoding before using it json_encode Function, the result is that this causes our Chinese content to be garbled, so the final use is to manually encrypt the data.

Achieve:

Want to achieve this function, the most important is to observe the characteristics of json data, 1 LZ summary can not be achieved completely json_encode The function of the function, referring to the information on the Internet, has achieved this function (that is, a recursive function):


function newArrayToJson($array)
{
  if(!is_array($array))
  {
    return '';
  }
  $func = __FUNCTION__;
  // The key judgment is whether it is an associative array, so as to decide whether it is needed or not json Encryption key And use []
  $associative = (array_keys($array) !== range(0, count($array) - 1)) ? true : false;
  if($associative && !empty($array))
  {
    $construct = array();
    foreach($array as $key => $value)
    {
      $key = '"'.$key.'"';
      if(is_array($value))
      {
        $value = $func($value);
      }
      elseif(!is_numeric($value))
      {
        $value = '"'.$value.'"';
      }
      $construct[] = "$key:$value";
    }
    $result = "{".implode(",",$construct)."}";
  }
  else
  {
    $construct = array();
    foreach($array as $value)
    {
      if(is_array($value))
      {
        $value = $func($value);
      }
      else if(!is_numeric($value))
      {
        $value = '"'.$value.'"';
      }
      $construct[] = $value;
    }
    $result = "[".implode(",", $construct)."]";
  }
  return $result;
}


// Test: 
$arr=array('1'=>'www.ofstack.com','2'=>'www.baidu.com','3'=>'www.sina.com.cn','4'=>' Script House ');
echo newArrayToJson($arr);
/*
 Run results: 
{"1":"www.ofstack.com","2":"www.baidu.com","3":"www.sina.com.cn","4":" Script House "}
*/

PS: Here we recommend several practical json online tools for your reference:

Online JSON code verification, verification, beautification and formatting tools:
http://tools.ofstack.com/code/json

JSON Online Formatting Tool:
http://tools.ofstack.com/code/jsonformat

Online XML/JSON interconversion tool:
http://tools.ofstack.com/code/xmljson

json code online formatting/beautification/compression/editing/conversion tool:
http://tools.ofstack.com/code/jsoncodeformat

C Language Style/HTML/CSS/json Code Formatting and Beautification Tool:
http://tools.ofstack.com/code/ccode_html_css_json

For more readers interested in PHP related contents, please check the special topics of this site: "Summary of json Format Data Operation Skills in PHP", "Summary of PHP Mathematical Operation Skills", "Introduction to PHP Basic Grammar", "Encyclopedia of PHP Array (Array) Operation Skills", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: